package com.yiba.vpn.service;


import com.yiba.vpn.util.EmailUtils;
import com.yiba.vpn.util.JavaQQMailUtils;
import com.yiba.vpn.util.SendEmail;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class MailService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    Configuration configuration; //freeMarker configuration

    @Autowired
    EmailProcessService emailProcessService;

    @Value("${gecco.mail.online}")
    private String MAIL_IS_ONLINE;

    public Map<String, String> sendHtmlMailUsingFreeMarker(Map<String, Object> model, String toemail, String freemarkid, Integer emailtype) throws IOException, TemplateException {
        Template t = configuration.getTemplate(freemarkid); // freeMarker template
        String content = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
        //这里是个发邮件的轮询策虐 先用 gmail 的发  不行用QQ
        if (emailtype != null) {
            MAIL_IS_ONLINE = emailtype + "";
        }
        if ("1".equals(MAIL_IS_ONLINE)) {
            //线上
            return emailProcessService.sendEmailLimitByAccount(toemail, "WFee candy", content, Integer.parseInt(MAIL_IS_ONLINE));
        } else {
            //本地邮件发送
            return emailProcessService.sendEmailLimitByAccount(toemail, "WFee candy", content, Integer.parseInt(MAIL_IS_ONLINE));
        }

    }

    public void sendHtmlMailUsingFreeMarkerNew(Map<String, Object> model, String toemail, String freemarkid, Integer emailtype) throws Exception {
        Template t = configuration.getTemplate(freemarkid); // freeMarker template
        String content = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
        if (emailtype == 1) {
            EmailUtils.master51Send(toemail, "51WFee candy", content);
        } else if (emailtype == 2) {
            EmailUtils.wy163Send(toemail, "163WFee candy", content);
        } else if (emailtype == 3) {
            EmailUtils.qqSend(toemail, "QQWFee candy", content);
        } else {
            EmailUtils.gmailSend(toemail, "WFee candygmail", content);
        }

    }

}
